package admin

import (
	"go-amis-admin/amis"
)

func NewDictModule(basePath string) *DictModule {
	var dictModule = &DictModule{
		CrudModule: NewCrudModule("数据字典", "dic", "fa fa-book", basePath, 300, DicDetail{}, nil, nil, nil),
	}
	dictModule.PageSchema["cssVars"] = amis.Options{
		"--Page-aside-width": "18rem",
	}
	dictModule.PageSchema["asideMinWidth"] = 330
	dictModule.PageSchema["asideMaxWidth"] = 460
	dictModule.PageSchema["asideResizor"] = true
	dictModule.PageSchema["aside"] = dictModule.GetAsideSchema()
	dictModule.UpdatePage()
	return dictModule
}

type DictModule struct {
	CrudModule[DicDetail]
}

func (d *DictModule) GetDetailByTypeApi(ctx *AdminContext) {
	typeCode := ctx.Query("code")
	var result []DicDetail
	d.DB.Where("Code=?", typeCode).Find(&result)
	ctx.Sucess(result, "")
}

func (d *DictModule) GetTypeListApi(ctx *AdminContext) {
	var result []DicType
	var count int64
	var searchForm amis.BaseQuery
	if err := ctx.ShouldBindQuery(&searchForm); err != nil {
		ctx.Fail(err.Error())
		return
	}
	tx := d.DB.Limit(searchForm.PerPage).Offset((searchForm.Page - 1) * searchForm.PerPage)
	if searchForm.Keywords != "" {
		tx.Where("label like ?", "%"+searchForm.Keywords+"%")
	}
	tx.Find(&result)
	d.DB.Model(&DicType{}).Count(&count)
	ctx.Sucess(amis.PageData{Items: result, Total: count}, "")
}

func (d *DictModule) GetTypeOptionsApi(ctx *AdminContext) {
	var result []DicType
	if err := d.DB.Find(&result).Error; err != nil {
		ctx.Fail(err.Error())
		return
	}
	var kv []amis.Options
	for _, dt := range result {
		kv = append(kv, amis.Options{"label": dt.Label, "value": dt.ID})
	}
	ctx.Sucess(kv, "")
}

func (d *DictModule) SaveDicTypeApi(ctx *AdminContext) {
	var dicType DicType
	if err := ctx.ShouldBindJSON(&dicType); err != nil {
		ctx.Fail(err.Error())
		return
	}
	if dicType.ID == 0 {
		if err := d.DB.Create(&dicType).Error; err != nil {
			ctx.Fail(err.Error())
			return
		}
	} else {
		if err := d.DB.Updates(&dicType).Error; err != nil {
			ctx.Fail(err.Error())
			return
		}
	}
	ctx.Sucess(dicType, "保存成功")
}

func (d *DictModule) DeleteTypeApi(ctx *AdminContext) {
	id := ctx.Query("id")
	if err := d.DB.Delete(&DicType{}, id).Error; err != nil {
		ctx.Fail(err.Error())
		return
	}
	ctx.Sucess(nil, "删除成功")
}

func (d *DictModule) GetAsideSchema() []amis.Obj {
	form := amis.New("dialog").
		Set("title", "新增类型").
		Set("body", amis.New("form").
			Set("api", "post:/admin/dic/saveDicType").
			Set("body", []amis.Obj{
				amis.New("hidden").Set("name", "ID"),
				amis.New("input-text").Set("name", "Label").Set("required", true).Set("label", "名称"),
				amis.New("input-text").Set("name", "Code").Set("required", true).Set("label", "编码"),
			}),
		)
	typeCrud := amis.New("crud").
		Set("api", "/admin/dic/getTypeList").
		Set("name", "typeCrud").
		Set("syncLocation", false).
		Set("itemAction", amis.Obj{
			"actionType": "reload",
			"target":     "crud?typeID=${ID}",
		}).
		Set("mode", "list").
		Set("listItem", amis.Obj{
			"title":    "${Label}",
			"subTitle": "${Code}",
			"actions": []amis.Obj{
				amis.New("button").Set("icon", "fa fa-pencil").Set("level", "link").
					Set("actionType", "dialog").Set("dialog", form),
				amis.New("button").Set("actionType", "ajax").
					Set("icon", "fa fa-times text-danger").Set("level", "link").
					Set("confirmText", "确认要删除？").Set("api", "delete:/admin/dic/deleteType?id=${ID}"),
			},
		}).
		Set("headerToolbar", []amis.Obj{
			amis.New("button").
				Set("icon", "fa fa-plus").
				Set("label", "新增类型").
				Set("level", "link").
				Set("actionType", "dialog").
				Set("dialog", form),
			amis.New("search-box").
				Set("name", "keywords").
				Set("align", "right"),
		}).
		Set("footerToolbar", []interface{}{"pagination"})

	return []amis.Obj{
		amis.New("panel").
			Set("title", "数据字典类型").
			Set("className", "border-0").
			Set("body", typeCrud),
	}
}
